<?php

namespace App\Admin\Controllers\Service;

use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class IndexController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '服务列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Service());

        $grid->column('id', ___('table.zy_service.Id'));
        $grid->column('user_id', ___('table.zy_service.User id'))->display(function ($id){
            $user = User::find($id);
            return $user?$user->name:'---';
        });
        $grid->column('category_id', ___('table.zy_service.Category id'))->display(function ($id){
           $category = ServiceCategory::find($id);
           if ($category) {
               return $category->name;
           }
        });
        $grid->column('name', ___('table.zy_service.Name'));
        $grid->column('price', ___('table.zy_service.Price'));
        $grid->column('time', ___('table.zy_service.Time'));
        $grid->column('remarks', ___('table.zy_service.Remarks'));
        $grid->column('status', ___('table.zy_service.Status'))->display(function ($id){
            return Service::$status[$id];
        })->switch([
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
        ]);
        $grid->column('create_time', ___('table.zy_service.Create time'));
        $grid->column('update_time', ___('table.zy_service.Update time'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Service::findOrFail($id));

        $show->field('id', ___('table.zy_service.Id'));
        $show->field('user_id', ___('table.zy_service.User id'));
        $show->field('category_id', ___('table.zy_service.Category id'));
        $show->field('name', ___('table.zy_service.Name'));
        $show->field('price', ___('table.zy_service.Price'));
        $show->field('time', ___('table.zy_service.Time'));
        $show->field('remarks', ___('table.zy_service.Remarks'));
        $show->field('status', ___('table.zy_service.Status'));
        $show->field('create_time', ___('table.zy_service.Create time'));
        $show->field('update_time', ___('table.zy_service.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Service());

        $form->select('user_id', ___('table.zy_service.User id'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/user')->help("输入真实用户姓名");;
        $form->select('category_id', ___('table.zy_service.Category id'))->options(function ($id) {
            $category = ServiceCategory::find($id);
            if ($category) {
                return [$category->id => $category->name];
            }
        })->ajax('/admin/ajax/service-category')->help("请输入服务分类");;
        $form->text('name', ___('table.zy_service.Name'));
        $form->decimal('price', ___('table.zy_service.Price'));
        $form->number('time', ___('table.zy_service.Time'));
        $form->textarea('remarks', ___('table.zy_service.Remarks'));
        $form->switch('status', ___('table.zy_service.Status'));
        //$form->number('create_time', ___('table.zy_service.Create time'));
        //$form->number('update_time', ___('table.zy_service.Update time'));

        return $form;
    }
}
